<template>
  <el-row :gutter="20">
    <!-- 左侧房产树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <CategoryTree @node-click="handleCategoryNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="设备名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
            >
            <el-button @click="resetQuery"
              ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
            >
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['iotmanage:equip-house:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button>
            <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['iotmanage:equip-house:export']"
            >
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
            <el-dropdown
              v-hasPermi="['iotmanage:equip-house:operate']"
              style="margin-left: 1vw"
              @command="handleCommand"
            >
              <el-button type="primary">
                <Icon icon="ep:setting" class="mr-5px" /> 表操作
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="install"
                    ><Icon icon="ep:circle-plus" />装表</el-dropdown-item
                  >
                  <el-dropdown-item divided command="change"
                    ><Icon icon="ep:warning" />换表</el-dropdown-item
                  >
                  <el-dropdown-item divided command="uninstall"
                    ><Icon icon="ep:circle-close" />拆表</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          highlight-current-row
          @row-click="handleCurrentRowClick"
          ref="equipTableRef"
        >
          <!-- <el-table-column label="id" align="center" prop="id" /> -->
          <!-- <el-table-column label="设备类型id" align="center" prop="equipTypeId" /> -->
          <el-table-column label="设备类型" align="center" prop="equipTypeName" />
          <el-table-column label="设备编号" align="center" prop="devNo" />
          <el-table-column label="设备编码" align="center" prop="devCode" />
          <el-table-column label="设备名称" align="center" prop="name">
            <template #default="scope">
              <el-link type="primary" :underline="false" @click="openDetail(scope.row.devNo)">{{
                scope.row.name
              }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="业务域" align="center" prop="busScope">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.IOTMANAGE_EQUIP_BUS_SCOPE" :value="scope.row.busScope" />
            </template>
          </el-table-column>
          <el-table-column label="所属产品" align="center" prop="refProdCode" />
          <el-table-column label="绑定网关设备" align="center" prop="wgDevCode" />
          <el-table-column label="最小量程" align="center" prop="minRange" />
          <el-table-column label="最大量程" align="center" prop="maxRange" />
          <el-table-column label="设备状态" align="center" prop="status1">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.IOTMANAGE_EQUIP_STATUS1" :value="scope.row.status1" />
            </template>
          </el-table-column>
          <el-table-column label="是否安装" align="center" prop="vdef1" min-width="90px">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.IOTMANAGE_EQUIP_VDEF1" :value="scope.row.vdef1"  />
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180px"
          />
          <el-table-column label="操作" align="center" min-width="120px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['iotmanage:equip-house:update']"
              >
                编辑
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['iotmanage:equip-house:delete']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>
  <!-- 表单弹窗：添加/修改 -->
  <EquipHouseForm ref="formRef" @success="getList" />
  <EquipRecForm ref="recFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { EquipHouseApi, EquipHouseVO } from '@/api/iotmanage/equiphouse'
import EquipHouseForm from './EquipHouseForm.vue'
import EquipRecForm from './EquipRecForm.vue'
import CategoryTree from './CategoryTree.vue'
import { DICT_TYPE } from '@/utils/dict'
import { ElTable } from 'element-plus'
const { push } = useRouter()

/** 房产设备关系 列表 */
defineOptions({ name: 'EquipHouse' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<EquipHouseVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  houseId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const equipTableRef = ref<InstanceType<typeof ElTable>>()
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EquipHouseApi.getEquipHousePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 处理房产分类被点击 */
const handleCategoryNodeClick = async (row) => {
  queryParams.houseId = row.id
  await getList()
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
/**
 * 表操作
 */
const recFormRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EquipHouseApi.deleteEquipHouse(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await EquipHouseApi.exportEquipHouse(queryParams)
    download.excel(data, '房产设备关系.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})

/**打开仪表报表 */
const openDetail = (devNo: string) => {
  push({ name: 'IoTEquipReport', params: { devNo } })
}

const handleCommand = (command: string) => {
  if (currentRow.value === undefined) {
    message.warning('请选择一条记录！')
    return
  }
  console.log(currentRow.value)
  recFormRef.value.open(command, currentRow.value)
}
/**
 * 当前行
 */
const currentRow = ref()
/**
 * 当前行点击事件
 */
const handleCurrentRowClick = (row: EquipHouseVO | undefined) => {
  currentRow.value = row
}
</script>
